package com.example.administrator.myapplication;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.administrator.myapplication.fragmentlei.Fragment1;
import com.example.administrator.myapplication.fragmentlei.Fragment2;
import com.example.administrator.myapplication.fragmentlei.Fragment3;

import java.util.ArrayList;

/**
 * Created by 董豪华 on 2017/1/3.
 */
public class ZhuJianMian extends AppCompatActivity{
    private BottomNavigationBar dibu;
    private ArrayList<Fragment> list;
    Fragment1 fragment1;
    Fragment2 fragment2;
    Fragment3 fragment3;
    Fragment mContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhujianmian);
        initview();

    }

    private void initview() {
        list = new ArrayList<>();
        fragment1 = new Fragment1(ZhuJianMian.this);
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
//        list.add(fragment1);
//        list.add(fragment2);
//        list.add(fragment3);
        dibu = (BottomNavigationBar) findViewById(R.id.dibu);
        dibu.addItem(new BottomNavigationItem(R.drawable.weixin,"精选"))
                .addItem(new BottomNavigationItem(R.drawable.lishi,"历史"))
                .addItem(new BottomNavigationItem(R.drawable.xinzuo,"星座"))
                .initialise();//对导航进行重绘
//        .setFirstSelectedPosition(0)//默认选择索引为0的菜单
        switchFrament(mContent,fragment1);
        dibu.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position){
                    case 0:
                        switchFrament(mContent,fragment1);
                        break;
                    case 1:
                        switchFrament(mContent,fragment2);
                        break;
                    case 2:
                        switchFrament(mContent,fragment3);
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {
            }
            @Override
            public void onTabReselected(int position) {
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder.create();
        View view = View.inflate(this, R.layout.dailog_duihua, null);
        Button quxiao = (Button) view.findViewById(R.id.bu_quxiao);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        Button queding = (Button) view.findViewById(R.id.bu_queding);
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        alertDialog.setView(view);
        alertDialog.show();
        return true;
    }
    /**
     *
     * @param from 刚显示的Fragment,马上就要被隐藏了
     * @param to 马上要切换到的Fragment，一会要显示
     */
    private void switchFrament(Fragment from,Fragment to) {
        if(from != to){
            mContent = to;
            android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            //才切换
            //判断有没有被添加
            if(!to.isAdded()){
                //to没有被添加
                //from隐藏
                if(from != null){
                    ft.hide(from);
                }
                //添加to
                if(to != null){
                    ft.add(R.id.boot,to).commit();
                }
            }else{
                //to已经被添加
                // from隐藏
                if(from != null){
                    ft.hide(from);
                }
                //显示to
                if(to != null){
                    ft.show(to).commit();
                }
            }
        }

    }

}